/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.provider.PageSliderAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * RecycleView图片滑动
 *
 * @since 2021-04-08
 */
public class BannerPagerSlice extends AbilitySlice {
    List<ImageViewInfo> itemList = new ArrayList<>();
    ArrayList<String> pages = new ArrayList<>();
    PageSlider pageSlider, pageVideoSlider;
    Text tv_count;
    private String position;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_banner_pager);
        initView();
        initData();
        if (intent != null) {
            position = intent.getStringParam("position");
            tv_count.setText(position + "" + "/" + itemList.size() + "");
        }
        pageSlider.setCurrentPage(Integer.parseInt(position) - 1);
    }

    private void initView() {
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageVideoSlider = (PageSlider) findComponentById(ResourceTable.Id_page_video_slider);
        pageSlider.setVisibility(Component.VISIBLE);
        pageVideoSlider.setVisibility(Component.HIDE);
        tv_count = (Text) findComponentById(ResourceTable.Id_tv_count);
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosPixles) {
            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                tv_count.setText(itemPos + 1 + "" + "/" + itemList.size() + "");
            }
        });
    }

    private void initData() {
        itemList.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069,864810829"
            + "&fm=26&gp=0.jpg"));
        itemList.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
            + ",3409802114&fm=26&gp=0.jpg"));
        itemList.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
        itemList.add(new ImageViewInfo("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2065870676,3251648368"
            + "&fm=26&gp=0.jpg"));
        itemList.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026"
            + ",3409802114&fm=26&gp=0.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170731/0838084002855326_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170731/0838084009134421_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170727/0847085702814554_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170802/0017030319134956_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170731/0838084023987260_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170731/0838084009134421_s.jpg"));
        itemList.add(new ImageViewInfo("https://img44.photophoto.cn/20170731/0838084002855326_s.jpg"));
        for (int i = 0; i < itemList.size(); i++) {
            pages.add(itemList.get(i).getUrl());
        }
        PageSliderAdapter adapter = new PageSliderAdapter(pages, this, this);
        pageSlider.setProvider(adapter);
    }

}
